#-------------------------------------------------------------------
# This file is part of the CMake build system for ogre-url
#-------------------------------------------------------------------

# Customise build
if (WIN32 OR APPLE)
	option (SETUP_OGRE_BUILD "Setup ENV_OGRE_BUILD variable" TRUE)
	option (SETUP_OGRE_SOURCE "Setup ENV_OGRE_BUILD variable" TRUE)
	option (SETUP_CURL_FORCE_STATICLIB "Force cURL static library detection" FALSE)
else ()
	option (SETUP_OGRE_BUILD "Setup ENV_OGRE_BUILD variable" FALSE)
	option (SETUP_OGRE_SOURCE "Setup ENV_OGRE_BUILD variable" FALSE)
endif (WIN32 OR APPLE)

# setup ogre build path
if (SETUP_OGRE_BUILD)
	set (SETUP_OGRE_BUILD_PATH "NOTFOUND" CACHE PATH "The ogre build path to use")
	set (OGRE_BUILD ${SETUP_OGRE_BUILD_PATH})
endif (SETUP_OGRE_BUILD)

# setup ogre source path
if (SETUP_OGRE_SOURCE)
	set (SETUP_OGRE_SOURCE_PATH "NOTFOUND" CACHE PATH "The ogre source path to use")
	set (OGRE_SOURCE ${SETUP_OGRE_SOURCE_PATH})
endif (SETUP_OGRE_SOURCE)

# check ogre version
option (SETUP_IGNORE_OGRE_VERSION "Ignore OGRE version" FALSE)

# lookinng for ogre
include (FindOGRE)

# is ogre found?
if (NOT OGRE_FOUND)
	message (FATAL_ERROR "Failed to find OGRE 3D instalation. Abort processing.")
endif (NOT OGRE_FOUND)

if (NOT SETUP_IGNORE_OGRE_VERSION)
	if (OGRE_VERSION_MAJOR EQUAL 1)
		if (OGRE_VERSION_MINOR EQUAL 7)
			set (OGRE_VERSION_VALID 1)
		elseif (OGRE_VERSION_MINOR LESS 7)
			set (OGRE_VERSION_VALID 0)
		else ()
			set (OGRE_VERSION_VALID 2)
		endif (OGRE_VERSION_MINOR EQUAL 7)
	elseif (OGRE_VERSION_MAJOR LESS 1)
		set (OGRE_VERSION_VALID 0)
	else ()
		set (OGRE_VERSION_VALID 2)
	endif (OGRE_VERSION_MAJOR EQUAL 1)
else ()
	set (OGRE_VERSION_VALID 1)
endif (NOT SETUP_IGNORE_OGRE_VERSION)

if (OGRE_VERSION_VALID EQUAL 0)
	set (error_msg "Invalid OGRE version. Only version 1.7 or greate are supported."
		"Please use correct OGRE version or disable OGRE version validating (SETUP_IGNORE_OGRE_VERSION)."
		"Abort processing."
	)
	message (FATAL_ERROR ${error_msg})
elseif (OGRE_VERSION_VALID GREATER 1)
	message (WARNING "OgreURL has never tested with selected Ogre version.")
endif (OGRE_VERSION_VALID EQUAL 0)

# check ogre build
if (OGRE_CONFIG_STATIC)
	message (FATAL_ERROR "Ogre satic build (OGRE_STATIC) does not supported yet. Please use shared ogre library instead.")
endif (OGRE_CONFIG_STATIC)

# check thread dependencies
if (OGRE_CONFIG_THREADS EQUAL 1)
	if (NOT OGRE_CONFIG_THREAD_PROVIDER EQUAL 1)
		message (FATAL_ERROR "Now only boost thread privider is supported by OgreURL.")
	endif (NOT OGRE_CONFIG_THREAD_PROVIDER EQUAL 1)
	set (BOOST_IS_REQUIRED TRUE)
else ()
	set (BOOST_IS_REQUIRED FALSE)
endif (OGRE_CONFIG_THREADS EQUAL 1)

# lookinng for boost
if (BOOST_IS_REQUIRED)
	# Find Boost
	# Prefer static linking in all cases
	if (WIN32 OR APPLE)
		set (Boost_USE_STATIC_LIBS TRUE)
	else ()
		# Statically linking boost to a dynamic Ogre build doesn't work on Linux 64bit
		set (Boost_USE_STATIC_LIBS ${OGRE_CONFIG_STATIC})
	endif (WIN32 OR APPLE)

	set (Boost_ADDITIONAL_VERSIONS "1.42" "1.42.0" "1.41.0" "1.41" "1.40.0" "1.40" "1.39.0" "1.39" "1.38.0" "1.38" "1.37.0" "1.37")
	
	set (SETUP_BOOST_INCLUDEDIR "" CACHE PATH "The BOOST root path to use")
	set (BOOST_INCLUDEDIR ${SETUP_BOOST_INCLUDEDIR})
	
	set (SETUP_BOOST_LIBRARYDIR "" CACHE PATH "The BOOST lib path to use")
	set (BOOST_LIBRARYDIR ${SETUP_BOOST_LIBRARYDIR})
	
	# Components that need linking (NB does not include header-only components like bind)
	set (BOOST_COMPONENTS thread date_time)
	find_package(Boost COMPONENTS ${BOOST_COMPONENTS} QUIET)
	if (NOT Boost_FOUND)
		# Try again with the other type of libs
		if(Boost_USE_STATIC_LIBS)
			set(Boost_USE_STATIC_LIBS)
		else()
			set(Boost_USE_STATIC_LIBS ON)
		endif()
		find_package(Boost COMPONENTS ${BOOST_COMPONENTS} QUIET)
	endif()
	find_package(Boost QUIET)
	
	if (NOT Boost_FOUND)
		message (FATAL_ERROR "Failed to find boost, please try to set corrent SETUP_BOOST_INCLUDEDIR/SETUP_BOOST_LIBRARYDIR.")
	endif (NOT Boost_FOUND)
	
	if (NOT Boost_THREAD_FOUND)
		message (FATAL_ERROR "Boost::thread not found, please try to set corrent SETUP_BOOST_INCLUDEDIR/SETUP_BOOST_LIBRARYDIR.")
	endif (NOT Boost_THREAD_FOUND)
	
	if (NOT Boost_THREAD_FOUND)
		message (FATAL_ERROR "Boost::date_time not found, please try to set corrent SETUP_BOOST_INCLUDEDIR/SETUP_BOOST_LIBRARYDIR.")
	endif (NOT Boost_THREAD_FOUND)
endif (BOOST_IS_REQUIRED)

# looking for cURL
message (STATUS "Looking for cURL...")
find_package(CURL REQUIRED)
message (STATUS "cURL include dirs: " ${CURL_INCLUDE_DIRS})
message (STATUS "cURL libraries: " ${CURL_LIBRARIES})

# install dependencies
include (InstallDependencies)

# install resources
include (InstallResources)
